
//
//  PhotoBrowserCell.m
//  kuangjia
//
//  Created by 张帅 on 15/10/14.
//  Copyright © 2015年 ZhangShuai. All rights reserved.
//

#import "PhotoBrowserCell.h"
#import <UIImageView+WebCache.h>
#import <SVProgressHUD.h>

@interface PhotoBrowserCell ()<UIScrollViewDelegate>


@property(nonatomic,strong) UIScrollView *scrollView;


@property(nonatomic,strong) UIActivityIndicatorView *indicator;


@end


@implementation PhotoBrowserCell



-(void)setUrl:(NSURL *)url{
    _url=url;
    
    
    [self resetScrollView];
    self.imageView.image =nil;
    [self.indicator startAnimating];
    // 之前 imageView 并没有指定大小
    // * RetryFailed 可以允许失败后重试
    // * RefreshCached 如果服务器的图像变化，而本地的图像是之前的图像，使用此选项，会更新服务器的图片
    //   GET 方法能够缓存，如果服务器返回的状态吗是 304，表示内容没有变化
    //   提交请求，如果是 304，就使用本地缓存，否则使用服务器返回的图片，具体内容看`网络视频`
    
    [self.imageView sd_setImageWithURL:url placeholderImage:nil options:SDWebImageRefreshCached|SDWebImageRetryFailed completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
        [self.indicator stopAnimating];
        // 执行到此处，表示图片已经下载完成
//        [self.imageView sizeToFit];
        if (error!=nil) {
        
            [SVProgressHUD showErrorWithStatus:@"您的网络不给力" maskType:1];
            return ;
        }
        
        
        CGSize imageSize = image.size;
        CGFloat bw = [UIScreen mainScreen].bounds.size.width;
        CGFloat bh = imageSize.height / imageSize.width * bw;
        self.imageView.frame = CGRectMake(0, 0, bw, bh);
        
        if (bh > [UIScreen mainScreen].bounds.size.height) {
            self.scrollView.contentSize = CGSizeMake(bw, bh);
        }
        else {
            CGFloat offsetY = ([UIScreen mainScreen].bounds.size.height - bh) * 0.5;
            self.scrollView.contentInset = UIEdgeInsetsMake(offsetY, 0, 0, 0);
        }
        
    }];

}

-(void)resetScrollView{

    self.scrollView.contentOffset=CGPointZero;
    
    self.scrollView.contentInset =UIEdgeInsetsZero;
    
    self.scrollView.contentSize = CGSizeZero;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setupUI];
    }
    return self;
}





-(void)setupUI{
    
    //添加kongjian
    
    [self.contentView addSubview:self.scrollView];
    
    [self.scrollView addSubview:self.imageView];
  
    [self.contentView addSubview:self.indicator];
    self.scrollView.translatesAutoresizingMaskIntoConstraints =NO;
    
    self.imageView.translatesAutoresizingMaskIntoConstraints=NO;
    self.indicator.translatesAutoresizingMaskIntoConstraints = NO;
    
    
    
    //scollView
    
    CGRect rect =self.bounds;
    
    rect.size.width-=20;
    
    self.scrollView.frame =rect;
    
    self.indicator.center =self.scrollView.center;
    [self prepareScrollView];
}


-(void)prepareScrollView{

    
    self.scrollView.delegate=self;
    self.scrollView.minimumZoomScale = 0.5;
    
    self.scrollView.maximumZoomScale = 3;

}


-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{

    return self.imageView;

}

-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale{
   
    
    CGFloat offsetY = (self.scrollView.bounds.size.height -self.imageView.frame.size.height)*0.5;
    
    
    
    CGFloat offsetX =(self.scrollView.bounds.size.width -self.imageView.frame.size.width)*0.5;
    
    
    offsetX =  offsetX<0 ? 0: offsetX;
    
    offsetY  = offsetY<0 ? 0:offsetY;
    
    self.scrollView.contentInset =UIEdgeInsetsMake(offsetY, offsetX, 0, 0);
//    NSLog(@"%@",view);
//    NSLog(@"%lf",scale);


    
    [self.photoDelegate photoBrowserCellEndZoom];
}



-(void)scrollViewDidZoom:(UIScrollView *)scrollView{

    
    [self.photoDelegate photoBrowserCellDidZoomingScale:self.imageView.transform.a];
    
  
}
-(UIActivityIndicatorView *)indicator{

    if (_indicator==nil) {
        _indicator =[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:2];
    }
    
    return _indicator;
}

-(UIImageView *)imageView{
    
    if (_imageView==nil) {
        
        _imageView=[[UIImageView alloc] init];
    }
    
    return _imageView;
}

-(UIScrollView *)scrollView{
    if (_scrollView==nil) {
        
        _scrollView=[[UIScrollView alloc] init];
        
    }
    return _scrollView;
}
@end
